package com.ruoyi.lxzx.service.impl;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lxzx.appDTO.lxjhDTO.UserActivitySaveDTO;
import com.ruoyi.lxzx.appVO.grzxVO.WdjhVO;
import com.ruoyi.lxzx.appVO.lxjhVO.hdjhDetailVO;
import com.ruoyi.lxzx.appVO.lxjhVO.hdjhVO;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListDetailVO;
import com.ruoyi.lxzx.domain.*;
import com.ruoyi.lxzx.service.IActivityGuestService;
import com.ruoyi.lxzx.service.IUserAttentService;
import com.ruoyi.lxzx.service.IUserCardService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.UserActivityMapper;
import com.ruoyi.lxzx.service.IUserActivityService;

/**
 * 活动Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
@AllArgsConstructor
public class UserActivityServiceImpl implements IUserActivityService {
    private UserActivityMapper userActivityMapper;
    private IActivityGuestService activityGuestService;
    private IUserAttentService userAttentService;
    private IUserCardService userCardService;


    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public UserActivity selectUserActivityById(Long id) {
        return userActivityMapper.selectUserActivityById(id);
    }

    /**
     * 查询活动列表
     *
     * @param userActivity 活动
     * @return 活动
     */
    @Override
    public List<UserActivity> selectUserActivityList(UserActivity userActivity) {
        return userActivityMapper.selectUserActivityList(userActivity);
    }

    /**
     * 新增活动
     *
     * @param userActivity 活动
     * @return 结果
     */
    @Override
    public int insertUserActivity(UserActivity userActivity) {
        return userActivityMapper.insertUserActivity(userActivity);
    }

    /**
     * 修改活动
     *
     * @param userActivity 活动
     * @return 结果
     */
    @Override
    public int updateUserActivity(UserActivity userActivity) {
        userActivity.setUpdateTime(DateUtils.getNowDate());
        return userActivityMapper.updateUserActivity(userActivity);
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteUserActivityByIds(Long[] ids) {
        return userActivityMapper.deleteUserActivityByIds(ids);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动主键
     * @return 结果
     */
    @Override
    public int deleteUserActivityById(Long id) {
        return userActivityMapper.deleteUserActivityById(id);
    }

    @Override
    public List<hdjhVO> hdjh() {
        return userActivityMapper.hdjh();
    }

    @Override
    public hdjhDetailVO hdjhxq(Long id) {
        UserActivity ua = userActivityMapper.hdjhxq(id);
        if(ua == null){
            throw  new ServiceException("没有获取到对应信息");
        }
        hdjhDetailVO result = new hdjhDetailVO();
        BeanUtils.copyProperties(ua, result);
        result.setId(ua.getId());
        List<UserCard> ucList = userCardService.findUserCardList(ua.getId());
        if (ucList != null && ucList.size() > 0) {
            List<String> ucImg = ucList.stream().map(UserCard::getImg).collect(Collectors.toList());
            result.setApplyList(ucImg);
            result.setApplyCount(ucImg.size());
        }
        LxlbListDetailVO createUser = userCardService.findUserCardByName(ua.getCreateBy());
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        result.setIsJoin("0");
        if(  0L !=finalUserId) {
            ucList.forEach(uc->{
                if(uc.getUserId().equals(SecurityUtils.getUserId())){
                    result.setIsJoin("1");
                }
            });
            if (finalUserId.equals(createUser.getUserId())) {
                createUser.setExchangeFlag(false);
            } else {
                createUser.setExchangeFlag(true);
                createUser.setGzStatus(userAttentService.gzStatus(createUser.getUserId(), finalUserId));
            }
        }else {
            createUser.setExchangeFlag(false);
            createUser.setGzStatus("0");
            result.setIsJoin("1");
        }
        result.setCreateUser(createUser);
        return result;
    }

    @Override
    public void fbhd(UserActivitySaveDTO userActivity) {
        String finalUserId = "0";
        try {
            finalUserId = SecurityUtils.getUserId()+"";
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserActivity entity = new UserActivity();
        BeanUtils.copyProperties(userActivity, entity);
        entity.setCreateBy(finalUserId);
        entity.setCreateTime(new Date());
        this.insertUserActivity(entity);
    }

    @Override
    public WdjhVO wdjh(Long userId) {
        WdjhVO vo = new WdjhVO();
        vo.setWcjd(userActivityMapper.wcjd(userId));
        vo.setWfbd(userActivityMapper.wfbd(userId));
        return vo;
    }

    @Override
    public void scjh(Long activityId) {
        Long finalUserId = 0L;
        Long[] adminArr = SecurityUtils.getAdminIds();
        try {
            finalUserId = SecurityUtils.getUserId();

        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserActivity ua = selectUserActivityById(activityId);
        Long finalUserId1 = finalUserId;
        Optional<Long> result = Arrays.stream(adminArr).filter(num -> num == finalUserId1).findFirst();

        if(ua == null){
            throw new ServiceException("没有对应的项目信息");
        } else if (!ua.getCreateBy().equals(finalUserId+"") && !result.isPresent()) {
            throw new ServiceException("对应的项目不是你所创建,不能删除");
        }else {
            userActivityMapper.deleteUserActivityById(activityId);
        }
    }
    @Override
    public void jhfill(Long activityId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserActivity ua = selectUserActivityById(activityId);
        if(ua == null){
            throw new ServiceException("没有对应的动态信息");
        } else {
            userActivityMapper.fillUserActivityById(activityId);
        }
    }
}
